package www.dianbaozu.com.ui.manage

import androidx.databinding.ObservableField
import androidx.hilt.lifecycle.ViewModelInject
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import okhttp3.MultipartBody
import www.dianbaozu.com.R
import www.wen.com.common.lib.base.module.BaseApiViewModel
import www.wen.com.common.lib.data.entities.*
import www.wen.com.common.lib.data.repository.AppDataManager
import www.wen.com.common.lib.extension.getString


/**
 * @author WEN
 * @Description:更新服务器头像
 * @date 2020//25 17:48
 */
class UpdateServerLogoViewModel @ViewModelInject constructor(appDataManager: AppDataManager) : BaseApiViewModel(appDataManager) {

    val mEnableUpdate = ObservableField(false)

    /**
     * 更新服务器头像
     */
    fun requestUpdateServerInfo(
            partMap: Map<String, okhttp3.RequestBody>,
            serverId: String,
            vararg files: MultipartBody.Part,
            action: ((ServerUpdateInfoEntity) -> Unit)? = null,
    ) {
        viewModelScope.launch {
            appDataManager.uploadOssFileFlow(partMap, *files)
                    .flatMapConcat {
                        appDataManager.updateServerInfoFlow(
                                serverId = serverId,
                                RequestBodyParameter(serverLogo = it.data.content[0], ))
                    }
                    .onStart {
                        loading.value = UiStateCustom(
                                stateData = LoadingDialog(
                                        getString(R.string.be_updating),
                                        LoadingDialogStateType.LOADING
                                )
                        )
                    }
                    .onCompletion { cause ->
                        if (cause == null) {
                            loading.value = UiStateCustom(
                                    stateData = LoadingDialog(
                                            tip = getString(R.string.update_successfully),
                                            loadingDialogStateType = LoadingDialogStateType.SUCCESS,
                                            isShowDialogTip = false,
                                            isShowToastTip = true
                                    )
                            )
                        }
                    }
                    .catch { cause ->
                        loading.value = UiStateCustom(
                                stateData = LoadingDialog(
                                        tip = cause.message ?: "网络异常",
                                        loadingDialogStateType = LoadingDialogStateType.ERROR,
                                        isShowDialogTip = false,
                                        isShowToastTip = true
                                )
                        )
                    }
                    .collect {
                        action?.invoke(it.data)
                    }
        }
    }

}


